package com.mszl.blog.service.impl;

import com.mszl.blog.entity.Category;
import com.mszl.blog.dao.CategoryDao;
import com.mszl.blog.entity.Tag;
import com.mszl.blog.service.CategoryService;
import com.mszl.blog.vo.CategoryVo;
import com.mszl.blog.vo.Result;
import com.mszl.blog.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageImpl;
//import org.springframework.data.domain.PageRequest;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2025-08-04 14:26:38
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;

    @Override
    public Category queryById(Integer id) {
        return this.categoryDao.queryById(id);
    }

//    @Override
//    public Page<Category> queryByPage(Category category, PageRequest pageRequest) {
//        long total = this.categoryDao.count(category);
//        return new PageImpl<>(this.categoryDao.queryAllByLimit(category, pageRequest), pageRequest, total);
//    }

    @Override
    public Category insert(Category category) {
        this.categoryDao.insert(category);
        return category;
    }

    @Override
    public Category update(Category category) {
        this.categoryDao.update(category);
        return this.queryById(category.getId());
    }

    @Override
    public boolean deleteById(Integer id) {
        return this.categoryDao.deleteById(id) > 0;
    }

    /**
     * 查询文章所有种类
     * @return
     */
    @Override
    public Result findAll() {
      List<Category> categories=categoryDao.selectList(new LambdaQueryWrapper<>());
      return Result.success(copyList(categories));

    }

    @Override
    public Result categoriesDetailById(Long id) {
        Category category=categoryDao.selectById(id);

        return Result.success(category);
    }

    private List<CategoryVo> copyList(List<Category> categoryList){
        List<CategoryVo> categoryVos=new ArrayList<>();
        for (Category category:categoryList ) {
           categoryVos.add(copy(category));
        }
        return  categoryVos;
    }


    private CategoryVo copy(Category category) {
       CategoryVo categoryVo=new CategoryVo();
       BeanUtils.copyProperties(category,categoryVo);
       categoryVo.setId(String.valueOf(category.getId()));
       return categoryVo;

    }
}
